/**
 * @file dboeuvre.c
 * @brief Fichier source du module DBoeuvre.
 *
 * @author Rachid Delory, Mamadou Keita, Florian Bernard
 */

/* *********************************************************************

   Librairies de database                          

   ********************************************************************* */

#include "dboeuvre.h"

/* *********************************************************************

   Fonctions                                      

   ********************************************************************* */

void recuperation_DOeu(Arbre *arbre, const int id)
{
  if(arbre)
    {
      DataBase data;
      ouvrir_Db();
      if(id == 0)
	{
	  /* Récupération de ceux qui n'ont pas de pere */
	  prepare_Db(&data, "select rowid, id_public, id_label, nbVisio, titre, type, illustration, commentaire, lienMedia, dateSortie from oeuvre where rowid not in (select id_oeuvre from regroupementOeuvre)");
	}
      else
	{
	  /* Récupération de ceux qui ont comme pere id */
	  prepare_Db(&data, "select rowid, id_public, id_label, nbVisio, titre, type, illustration, commentaire, lienMedia, dateSortie from oeuvre where rowid in (select id_oeuvre from regroupementOeuvre where id_regroupement = ?)");
	  bindValueInt_Db(&data, 1, id);
	}
      while(execute_Db(&data))
	{
	  Oeuvre oeuvre;
	  Date *date = NULL;
	  Label *label = NULL;
	  int idRec;
	  init_Oeu(&oeuvre);
	  setId_Oeu(&oeuvre, getValueInt_Db(&data, 0));
	  
	  setPublic_Oeu(&oeuvre, getValueInt_Db(&data, 1));
	  
	  label = creer_Lab();
	  
	  recuperation_DLab(&label, getValueInt_Db(&data, 2));
	  setLabel_Oeu(&oeuvre, label);
	  detruire_Lab(&label);
	  
	  setNbVisio_Oeu(&oeuvre, getValueInt_Db(&data, 3));
	  setTitre_Oeu(&oeuvre, getValueText_Db(&data, 4));
	  setType_Oeu(&oeuvre, getValueInt_Db(&data, 5));
	  setIllustration_Oeu(&oeuvre, getValueText_Db(&data, 6));
	  setCommentaire_Oeu(&oeuvre, getValueText_Db(&data, 7));
	  setLienMedia_Oeu(&oeuvre, getValueText_Db(&data, 8));
	  idRec = getValueInt_Db(&data, 9);
	  recuperation_DDat(&date, idRec);
	  setDateSortie_Oeu(&oeuvre, date);
	  detruire_Dat(&date);
	  recuperation_DGen(getEnsembleGenre_Oeu(&oeuvre), getId_Oeu(&oeuvre), OEUVRE_DGEN);
	  
	  recuperation_DAct(getEnsembleCollaborateur_Oeu(&oeuvre), getId_Oeu(&oeuvre));
	  
	  switch(getType_Oeu(&oeuvre))
	    {
	    case FILM_OEU:
	      recuperation_DFil(&oeuvre, getId_Oeu(&oeuvre));	      
	      break;
	    case MUSIQUE_OEU:
	      recuperation_DMus(&oeuvre, getId_Oeu(&oeuvre));
	      break;
	    case LIVRE_OEU:
	      recuperation_DLiv(&oeuvre, getId_Oeu(&oeuvre));
	      break;
	    default:
	      break;
	    }
	  
	  ajoutFeuille_Arb(arbre, &oeuvre);
	  liberer_Oeu(&oeuvre);

	}
      finalise_Db(&data);
    }
}

void insertion_DOeu(Oeuvre *oeuvre)
{
  if(oeuvre)
    {
      DataBase data;
      int b = 0;
      ouvrir_Db();
      if(getSup_Oeu(oeuvre) == 1)
	{
	  prepare_Db(&data, "delete from oeuvre where rowid = ?");
	  bindValueInt_Db(&data, 1, getId_Oeu(oeuvre));
	}
      else
	{
	  insertion_DDat(getDateSortie_Oeu(oeuvre));   
	  if(getId_Oeu(oeuvre) == 0)
	    {
	      prepare_Db(&data, "insert into oeuvre (id_public, id_label, nbVisio, titre, type, illustration, commentaire, dateSortie) values (?, ?, ?, ?, ?, ?, ?, ?)");
	    }
	  else
	    {
	      b = 1;
	      if(getMaj_Oeu(oeuvre) == 1)
		{
		  prepare_Db(&data, "update oeuvre set id_public = ?, id_label = ?, nbVisio = ?, titre = ?, type = ?, illustration = ?, commentaire = ?, dateSortie = ? where rowid = ?");
		  bindValueInt_Db(&data, 9, getId_Oeu(oeuvre));
		}
	    }
	  if(getMaj_Oeu(oeuvre) == 1 || getId_Oeu(oeuvre) == 0)
	    {
	      bindValueInt_Db(&data,1,getPublic_Oeu((oeuvre)));
	      bindValueInt_Db(&data,2,getId_Lab(getLabel_Oeu((oeuvre))));
	      bindValueInt_Db(&data,3,getNbVisio_Oeu(oeuvre));
	      bindValueText_Db(&data,4,getTitre_Oeu(oeuvre));
	      bindValueInt_Db(&data,5,getType_Oeu(oeuvre));
	      bindValueText_Db(&data,6,getIllustration_Oeu(oeuvre));
	      bindValueText_Db(&data,7,getCommentaire_Oeu(oeuvre));
	      bindValueInt_Db(&data,8,getId_Dat(getDateSortie_Oeu(oeuvre)));
	    }
	}
      if(getMaj_Oeu(oeuvre) == 1 || getId_Oeu(oeuvre) == 0 || getSup_Oeu(oeuvre) == 1)
	{
	  if(execute_Db(&data))
	    {
	    }
	  finalise_Db(&data);
	}
      
      if(getSup_Oeu(oeuvre) == 0 && getId_Oeu(oeuvre) == 0)
	{
	  prepare_Db(&data, "select max(rowid) from oeuvre");
	  if(execute_Db(&data))
	    {
	      setId_Oeu(oeuvre, getValueInt_Db(&data, 0));
	    }
	}
      
      switch(getType_Oeu(oeuvre))
	{
	case FILM_OEU:
	  if(!b)
	    setId_Fil(getMedia_Oeu(oeuvre), getId_Oeu(oeuvre));
	  setSup_Fil((Film *) getMedia_Oeu(oeuvre),getSup_Oeu(oeuvre));
	  insertion_DFil(getMedia_Oeu(oeuvre), b);
	  break;
	case MUSIQUE_OEU:
	  if(!b)
	    setId_Mus(getMedia_Oeu(oeuvre), getId_Oeu(oeuvre));
	  setSup_Mus((Musique *) getMedia_Oeu(oeuvre),getSup_Oeu(oeuvre));
	  insertion_DMus(getMedia_Oeu(oeuvre), b);
	  break;
	case LIVRE_OEU:
	  if(!b)
	    setId_Liv(getMedia_Oeu(oeuvre), getId_Oeu(oeuvre));
	  setSup_Liv((Livre *) getMedia_Oeu(oeuvre),getSup_Oeu(oeuvre));
	  insertion_DLiv(getMedia_Oeu(oeuvre), b);
	  break;
	}
      {
	if(getSup_Oeu(oeuvre) == 1)
	  {
	    prepare_Db(&data, "delete from oeuvregenre where id_oeuvre = ?");
	    bindValueInt_Db(&data, 1, getId_Oeu(oeuvre));
	    if(execute_Db(&data))
	      {
	      }
	    finalise_Db(&data);
	  }
	else
	  {
	    Ensemble *ens;
	    int tailleEns, i;

	    ens = getEnsembleGenre_Oeu(oeuvre);
	    tailleEns = getTaille_Ens(ens);
	    prems_Ens(ens);
	    for(i = 0; i < tailleEns; i++)
	      {
		if(getSup_Ens(ens) == 1)
		  {
		    prepare_Db(&data, "delete from oeuvregenre where id_oeuvre = ? and id_genre = ?");
		  }
		else if(getMaj_Ens(ens) == 1)
		  {
		    prepare_Db(&data, "insert into oeuvregenre (id_oeuvre, id_genre) values(?, ?)");
		  }
		if(getSup_Ens(ens) == 1 || getMaj_Ens(ens) == 1)
		  {
		    bindValueInt_Db(&data, 1, getId_Oeu(oeuvre));
		    bindValueInt_Db(&data, 2, getId_Gen(getData_Ens(ens)));
		    if(execute_Db(&data))
		      {
		    
		      }
		    finalise_Db(&data);
		  }
		suivant_Ens(ens);
	      }
	  }
      }
      
      if(getSup_Oeu(oeuvre) == 1)
	{
	  prepare_Db(&data, "delete from activite where id_oeuvre = ?");
	  bindValueInt_Db(&data, 1, getId_Oeu(oeuvre));
	  if(execute_Db(&data))
	    {

	    }
	  finalise_Db(&data);
	}
      else
	{
	  insertion_DAct(getEnsembleCollaborateur_Oeu(oeuvre), getId_Oeu(oeuvre));
	}
    }
}

void setVue_DOeu(Oeuvre * oeuvre, const int b)
{
  if(oeuvre)
    {
      DataBase data;

      prepare_Db(&data, "update oeuvre set NbVisio = ? where id = ?");
      bindValueInt_Db(&data,1,b);
      bindValueInt_Db(&data,2,getId_Oeu(oeuvre));
      if (execute_Db(&data))
	{
	}
      finalise_Db(&data);
    }
}
